package com.itheima.tlias84.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.itheima.tlias84.config.AliyunOssConfig;
import com.itheima.tlias84.config.OssClient;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {
//    @Resource
//    AliyunOssConfig config;

    @Resource
    OssClient ossClient;


    /**
     * MultipartFile Spring封装的用于接收文件的对象
     * @param file
     * @return
     */
    @PostMapping
    public ResultUtil upload(MultipartFile file) throws IOException {

        //AccessKey ID    LTAI5t9YCCgy8xmAyLgV6rAQ
        //AccessKey Secret    7Ze7kU3jOUSOVM0Fxzeq1pEluVr2SD
        //bucket test-timscy
        //获取参数名
        String name = file.getName();
        //获取实际上传文件名
        String originalFilename = file.getOriginalFilename();
        log.info("name{}",name);
        log.info("OriginalName{}",originalFilename);
        String originalFilename1 = file.getOriginalFilename();

        String url = ossClient.upload(originalFilename1, file.getInputStream());
        return ResultUtil.success("上传成功",url);
//        int i = originalFilename1.lastIndexOf(".");
//        String suffix = originalFilename1.substring(i);
        //阿里云oss访问路径
//        String endpoint = config.getEndpoint();

        // 填写Bucket名称，例如examplebucket。
//        String bucketName = config.getBucketName();

        //生成不重复id
//        String uuid = UUID.randomUUID().toString();

        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
//        String objectName = uuid+suffix;
        //转移文件到阿里云oss
        // 创建OSSClient实例。
//        OSS ossClient = new OSSClientBuilder().build(endpoint, config.getAccessKeyId(),
//                config.getSecretAccessKey());

        /*
        1总结阿里云oss上传流程
        2.书写上传工具类 OssUtil
         */
    }
}
